package org.akomantoso.core.data;

import java.io.IOException;
import java.util.Calendar;

import org.akomantoso.core.data.attribute.Date;
import org.akomantoso.core.data.attribute.Name;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.OutputUtil;
import org.akomantoso.core.io.util.DateUtil;

/**
 * The element FRBRdate is the metadata property containing a relevant date of
 * the document in the respective level of the FRBR hierarchy. Attribute name
 * specifies which actual date is contained here.
 * 
 * <pre>
 * <xsd:element name="FRBRdate">
 *   <xsd:complexType>
 * 		<xsd:complexContent>
 * 			<xsd:extension base="metaopt">
 * 				<xsd:attributeGroup ref="date"/>
 * 				<xsd:attributeGroup ref="name"/>
 * 			</xsd:extension>
 * 		</xsd:complexContent>
 *   </xsd:complexType>
 * </xsd:element>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public final class FRBRdate extends MetaOpt implements Date , Name{

	public static final String ELEMENT = "FRBRdate";
	
	private Calendar _date;
	private String _name;
	
	/**
	 * @see org.akomantoso.core.data.attribute.Name#getName()
	 */
	public String getName() {
		return _name;
	}
	
	/**
	 * @see org.akomantoso.core.data.attribute.Name#setName(java.lang.String)
	 */
	public void setName(String name) {
		_name = name;
	}
	
	/**
	 * @see org.akomantoso.core.data.attribute.Date#getDate()
	 */
	public Calendar getDate() {
		return _date;
	}
	
	/**
	 * @see org.akomantoso.core.data.attribute.Date#setDate(java.util.Calendar)
	 */
	public void setDate(Calendar date) {
		_date = date;
	}
	
	/**
	 * @see org.akomantoso.core.data.MetaOpt#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		out.writeStart(ELEMENT);
		OutputUtil.writeDate(out, this);
		OutputUtil.writeName(out, this);
		super.writeExternal(out);
		out.writeEnd();
	}

	/**
	 * @see org.akomantoso.core.data.MetaOpt#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		super.readExternal(in);
		_name = in.readAttribute(Name.ATTRIBUTE);
		_date = DateUtil.convert(in.readAttribute(Date.ATTRIBUTE));
	}
	
}